package com.cyber.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyber.entity.Member;
import com.cyber.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jsf
 * @since 2022-02-21
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private IMemberService ms;
    @GetMapping("/selectAll")
    public List<Member> selectAll(){
        return ms.list();
    }

    @PostMapping("/add")
    public void add(@RequestBody Member member){
        ms.save(member);
    }
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        ms.removeById(id);
    }
    @GetMapping("/selectOne/{id}")
    public Member selectOne(@PathVariable("id") int id){
        return ms.getById(id);
    }
    @GetMapping("/selectByName/{name}")
    public Member selectByName(@PathVariable("name")String name){
        QueryWrapper<Member> wrapper=new QueryWrapper<>();
        wrapper.eq("name",name);
        return ms.getOne(wrapper);
    }
    @PutMapping("/update")
    public void update(@RequestBody Member member){
        ms.updateById(member);
    }

    @GetMapping("/login/{username}/{password}/{role}")
    public boolean login(@PathVariable("username") String username,
                         @PathVariable("password") String password,
                         @PathVariable("role") int role,
                         HttpSession session){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",username);
        wrapper.eq("pwd",password);
        Object one=null;
        if(role==1){
            one = ms.getOne(wrapper);
            if(one==null){
                return false;
            }
            Member m=(Member)one;
            if(m.getMoney()<10){
                return false;
            }else{
                m.setMoney(m.getMoney()-10);
                ms.updateById(m);
            }
            session.setAttribute("user",one);
        }
        if (role==2){
            if("admin".equals(username)&&"admin".equals(password)){
                return true;
            }
        }

        return one!=null;
    }
    @GetMapping("/updateMoney")
    public void updateMoney(String name,double money){
        QueryWrapper<Member> wrapper=new QueryWrapper<>();
        wrapper.eq("name",name);
        Member m = ms.getOne(wrapper);
        m.setMoney(m.getMoney()-money);
        ms.updateById(m);
    }
    @GetMapping("/selectMoney/{money}")
    public boolean updateMoney(@PathVariable("money") double money,HttpSession session){
        Member m=(Member) session.getAttribute("user");
        m = ms.getById(m.getId());
        return m.getMoney()>=money;
    }
    @PutMapping("/updatePwd")
    public void updatePwd(String name,String pwd,HttpSession session){
        Member m=(Member) session.getAttribute("user");
        m = ms.getById(m.getId());
        m.setPwd(pwd);
        ms.updateById(m);
    }
    @PutMapping("/chongzhi")
    public void chongzhi(double money,int id){
        Member m = ms.getById(id);
        m.setMoney(m.getMoney()+money);
        ms.updateById(m);
    }
}

